package com.cy.pj.goods.dao;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.sql.DataSource;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class DefaultGoodsDao implements GoodsDao{

    private static final Logger log= LoggerFactory.getLogger(DefaultGoodsDao.class);

    @Autowired
    private DataSource dataSource;

    //行映射
    Map<String,Object> rowmap(ResultSetMetaData metaData,ResultSet rs) throws SQLException {
        Map<String, Object> map = new HashMap<>();
        for (int i=1;i<=metaData.getColumnCount();i++){
            map.put(metaData.getColumnName(i), rs.getObject(metaData.getColumnName(i)));
        }
        return map;
    }
    //释放资源
    private void close(ResultSet rs,Statement st,Connection conn){
        if (rs!=null)try {
            rs.close();
        }catch (Exception e1){
            e1.printStackTrace();
        }
        if (st!=null)try {
            st.close();
        }catch (Exception e1){
            e1.printStackTrace();
        }
        if (conn!=null)try {
            conn.close();
        }catch (Exception e1){
            e1.printStackTrace();
        }
    }

    @Override
    public List<Map<String, Object>> findGoods() {
        String sql="select * from tb_goods";
        Connection conn=null;
        Statement st=null;
        ResultSet rs=null;
        try {
            //1,获取连接
            conn = dataSource.getConnection();
            //2,创建Statement
            st = conn.createStatement();
            //3,发送sql
            rs = st.executeQuery(sql);
            //4.处理结果
            List<Map<String,Object>> list = new ArrayList<>();
            ResultSetMetaData metaData = rs.getMetaData();
            while (rs.next()){
//                Map<String, Object> map = new HashMap<>();
//                map.put("id", rs.getInt("id"));
//                map.put("name", rs.getString("name"));
//                map.put("remark", rs.getString("remark"));
//                map.put("createdTime", rs.getTimestamp("createdTime"));
//                list.add(map);
                list.add(rowmap(metaData,rs));
            }
            return list;
        }catch (Exception e){
//            e.printStackTrace();
//            return null;
            log.error("查询异常,{}",e.getMessage());
            throw new RuntimeException("数据查询失败:"+e.getMessage());
        }finally {
            //5,释放资源
//            if (rs!=null)try {
//                rs.close();
//            }catch (Exception e1){
//                e1.printStackTrace();
//            }
//            if (st!=null)try {
//                st.close();
//            }catch (Exception e1){
//                e1.printStackTrace();
//            }
//            if (conn!=null)try {
//                conn.close();
//            }catch (Exception e1){
//                e1.printStackTrace();
//            }
            close(rs, st, conn);
        }
    }
}
